﻿using FlowDesigner.ExchangeDialog;
using FlowDesigner.INotifyCommadBase;
using FlowDesigner.Interfacebase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Unity;

namespace FlowDesigner.ViewModel
{
    /// <summary>
    /// 对话框父窗体
    /// </summary>
    public class BaseDialogViewModel : IBaseDialogViewModel
    {
        public UserControl DialogView {  get; set; }
        public Command Cancelcommand { get; set; }
        public Command Savecommand { get; set; }
        private readonly BaseDialog basedialog;
        public BaseDialogViewModel(BaseDialog baseDialog) 
        {
            Cancelcommand = new Command(Cancel);
            Savecommand = new Command(Save);
            baseDialog.DataContext = this;
            this.basedialog = baseDialog;
        }

        event Func<object> IBaseDialogViewModel.MethodCalled
        {
            add
            {
                throw new NotImplementedException();
            }

            remove
            {
                throw new NotImplementedException();
            }
        }

        public string Title { get; set; }

        public event Action<object> MethodCalled;

        /// <summary>
        /// 关闭窗口按钮
        /// </summary>
        /// <param name="o"></param>
        public void Cancel(object o)
        {
            (o as Window)?.Close();
            DialogView = null;
        }/// <summary>
         /// 
         /// </summary>
         /// <param name="o"></param>
        public void Save(object o)
        {
             //if(Howwindowopens)
            (o as Window)?.Close();
            var getAction = (DialogView.DataContext as IBaseDialogViewModel).returnData;//这里调用returnData 获取具体ViewModel 传过来的值
            if (getAction != null)
            {
                MethodCalled.Invoke(getAction());//调用实际挂载的委托触发回调 拿到具体参数
                DialogView = null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="View_">展示的内容</param>
        /// <param name="Model">关联的Model</param>
        /// <param name="o">回调的方法</param>
        /// <returns></returns>
        public bool ShowDialog(Type View_, Type Model,Action<object> o,object parameter)
        {
            //var basedialog=  Containerfot.Container.container.Resolve<BaseDialog>();
            if (basedialog != null)
            {
                var viewdata= Containerfot.Container.container.Resolve(View_) as UserControl;
                viewdata.DataContext = Containerfot.Container.container.Resolve(Model);
                DialogView= viewdata;
               (Containerfot.Container.container.Resolve(Model) as IBaseDialogViewModel).Passparameters(parameter);//
                MethodCalled = o;
                basedialog.ShowDialog();
            }
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="View_">展示的内容</param>
        /// <param name="Model">关联的Model</param>
        /// <param name="o">回调的方法</param>
        /// <returns></returns>
        public bool Show(Type View_, Type Model, Action<object> o, object parameter)
        {
            //var basedialog = Containerfot.Container.container.Resolve<BaseDialog>();
            if (basedialog != null)
            {
                basedialog.AllowsTransparency = false;
                basedialog.WindowStyle= WindowStyle.SingleBorderWindow;
                basedialog.Background = new SolidColorBrush(Colors.White);
                var viewdata = Containerfot.Container.container.Resolve(View_) as UserControl;
                viewdata.DataContext = Containerfot.Container.container.Resolve(Model);
                DialogView = viewdata;
                (Containerfot.Container.container.Resolve(Model) as IBaseDialogViewModel).Passparameters(parameter);//
                MethodCalled = o;
                basedialog.Show();
            }
            return false;
        }
        public void Passparameters(object o)
        {
            throw new NotImplementedException();
        }

        public object returnData()
        {
            throw new NotImplementedException();
        }
    }
}
